// import Im from '@/commen/tim/im'
const user = {
	state: {
		currentUserProfile: {},
		isLogin: false,
		isSDKReady: false, // TIM SDK 是否 ready
		userID: 0,
		userSig: '',
		sdkAppID: 0,
	},
	mutations: {
		updateCurrentUserProfile(state, userProfile) {
			state.currentUserProfile = userProfile
		},
		toggleIsLogin(state, isLogin) {
			console.log("登录成功", isLogin);
			state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
		},
		toggleIsSDKReady(state, isSDKReady) {
			console.log('变更了状态', isSDKReady);
			state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
			
			console.log("isSDKReady", state.isSDKReady);
		},
		reset(state) {
			Object.assign(state, {
				vuex_user: {},
				currentUserProfile: {},
				isLogin: false,
				isSDKReady: false // TIM SDK 是否 ready
			})
		},
		checkUserLogin(content) {
			//检查用户信息是否存在
			if (JSON.stringify(content.vuex_user) == '{}') {
				uni.reLaunch({
					url: '/pages/login/index'
				});
				return;
			}
		},
	},
	actions: {
		login(context, userInfo) {
			console.log('来登录了');
			console.log(userInfo);
			tim.login({
				userID: userInfo.timUserId,
				userSig: userInfo.timUserSig
			}).then(() => {
				context.commit('toggleIsLogin', true);
			}).catch(imError => {
				if (imError.code === 2000) {
					uni.showToast({
						title: '请检查是否正确填写了 SDKAPPID',
						icon: 'none'
					});
				} else {
					uni.showToast({
						title: imError.message,
						icon: 'none'
					});
				}
			});

			Im.login({
			  userId: userInfo.timUserId,
			  userSig: userInfo.timUserSig
			}, ret => {
			  console.log(ret);
			});
		},
		logout(context) {
			// 若有当前会话，在退出登录时已读上报
			// if (context.rootState.conversation.currentConversation.conversationID) {
			// 	tim.setMessageRead({
			// 		conversationID: context.rootState.conversation.currentConversation.conversationID
			// 	})
			// }
			// tim.logout();
			// Im.logout({}, ret => {});
			context.commit('toggleIsLogin')
			// context.commit('stopComputeCurrent')
			context.commit('reset')
			uni.removeStorageSync('userInfo');
			uni.setTabBarItem({
				index: 1,
				iconPath: '/static/images/tabbar/repair.png',
				selectedIconPath: '/static/images/tabbar/repair-fill.png',
				pagePath: '/pages/client/repair/index'
			});
			uni.reLaunch({
				url: '/pages/home/index'
			});
		},
	}
}

export default user
